﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SportsStore.Domain.Entities
{
    public class Cart 
    {
        private List<CartLine> LineCollection { get; set; }

        public Cart()
        {
            this.LineCollection = new List<CartLine>();
        }

        public void AddItem( Product product, int quantity )
        {
            var line = this.LineCollection
                .Where( cartLine => cartLine.Product.ProductID == product.ProductID )
                .FirstOrDefault();

            if ( line == null )
                this.LineCollection.Add( new CartLine
                    {
                        Product = product,
                        Quantity = quantity
                    } );
            else
                line.Quantity += quantity;
        }

        public void RemoveLine( Product product )
        {
            this.LineCollection.RemoveAll( line => line.Product.ProductID == product.ProductID );
        }

        public decimal ComputeTotalValue()
        {
            return this.LineCollection.Sum( line => line.Product.Price * line.Quantity );
        }

        public void Clear()
        {
            this.LineCollection.Clear();
        }

        public IEnumerable<CartLine> Lines
        {
            get { return this.LineCollection; }
        }
    }

    public class CartLine
    {
        public Product Product { get; set; }
        public int Quantity { get; set; }
    }
}
